import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
const moduleA = {
  //需要保存的数据
  state: {
    countA: 1
  },
  //如果需要修改store中的值唯一的方法就是提交mutation来修改，变更
  mutations: {
    incrementA(state, id) {
      // state.countA++;
      state.countA += id;
    }
  },
  //提交一个actions，在actions中提交mutation再去修改状态值
  actions: {
    incrementAFun(context, id) {
      context.commit("incrementA", id);
    }
  },
  //Getter相当于vue中的computed计算属性，getter 的返回值会根据它的依赖被缓存起来,计算不变更
  getters: {
    doubleCountA(state) {
      return state.countA * 2;
    }
  }
};
const moduleB = {
  state: {
    countB: 0
  },
  mutations: {
    incrementB(state) {
      state.countB++;
    }
  },
  actions: {
    incrementBFun(context) {
      context.commit("incrementB");
    }
  },
  getters: {
    doubleCountB(state) {
      return state.countB * 2;
    }
  }
};
export default new Vuex.Store({
  state: {
    count: 11
  },
  mutations: {
    increment(state) {
      state.count++;
    }
  },
  actions: {},
  modules: {
    a: moduleA,
    b: moduleB
  }
});
